#include "DefaultHandlerImp.h"

#include <iostream>

namespace frame {
namespace imp {

DefaultHandlerImp * DefaultHandlerImp::__inst = nullptr;

DefaultHandlerImp::DefaultHandlerImp()
:HandlerImp()
{
}

void DefaultHandlerImp::connected(ServerChannelImp * serverChannelImp){
    std::cout << "connected endpoint:" << serverChannelImp->getRemoteIp() << ":" << serverChannelImp->getRemotePort() << std::endl;
}
void DefaultHandlerImp::disconnected(ServerChannelImp * serverChannelImp, Error error){
    std::cout << "disconnected endpoint:[" << serverChannelImp->getId() << "], error code:[" << error << "]" << std::endl;
}
void DefaultHandlerImp::read(ServerChannelImp * serverChannelImp, const char * buf,  size_t size){
    std::cout << "read OK, id:[" << serverChannelImp->getId() 
        << "], bytes_transferred:[" << size 
        << "], read data:[" << std::string(buf,size) 
        << "]" << std::endl;


    // decode && biz code callback 
    if(buf[0] == '1'){ // 1XXXX  write remote helloworld
        serverChannelImp->write("HelloWorld", 10);
    }
    if(buf[0] == 'a'){  // aXXXXX for end remote connection
        serverChannelImp->disconnect();
    }
    else{

    }
}
void DefaultHandlerImp::readException(ServerChannelImp * serverChannelImp, int error){
    std::cout << "read exception " << serverChannelImp->getId() << std::endl;
}
void DefaultHandlerImp::writed(ServerChannelImp * serverChannelImp, size_t size){
    std::cout << "writed OK, id:[" << serverChannelImp->getId() <<"], bytes_transferred:" << size << std::endl;
}
void DefaultHandlerImp::writeException(ServerChannelImp* serverChannelImp, int error){
    std::cout << "write exception " << serverChannelImp->getId() << std::endl;
}
DefaultHandlerImp * DefaultHandlerImp::instance(){
    if(DefaultHandlerImp::__inst == nullptr){
        DefaultHandlerImp::__inst = new DefaultHandlerImp();
    }
    return DefaultHandlerImp::__inst;
}

} //end of namespace  imp
} //end of namespace  frame
